

#import "BaseDataManager.h"
@implementation BaseDataManager



- (void)saveInfo:(id)obj toFile:(NSString *)filePath{
    [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
    }
    NSError *error = nil;
    NSData *json_data = [NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:&error];
    if (error) {
        LYLog(@"%@--%@",error,filePath);
    }
    [json_data writeToFile:filePath atomically:YES];
}

- (id)readFileDataAtPath:(NSString *)filePath{
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSError *error = nil;
    if (data != nil){
        id infos = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        if (error) {
            LYLog(@"%@--%@",error,filePath);
        }
        return infos;
    }
    return nil;
}

+ (NSString *)saveImageWithUrl:(NSURL *)imageURL toFile:(NSString *)filePath imageName:(NSString *)imageName{
    [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *imgName = imageName;
    if (!imageName) {
        NSTimeInterval timeInterval = [NSDate date].timeIntervalSince1970;
        imgName = [NSString stringWithFormat:@"image_%.0f",timeInterval];
    }
    NSError *error = nil;
    NSString *image_file = [filePath stringByAppendingPathComponent:imgName];
    if (imageURL) {
        [[NSFileManager defaultManager] copyItemAtURL:imageURL toURL:[NSURL fileURLWithPath:image_file] error:&error];
        if (error){
            LYLog(@"image_file - %@",error);
        }
    }
    return imgName;
}

+ (NSString *)saveImageWithData:(UIImage *)image toFile:(NSString *)filePath imageName:(NSString *)imageName{
    if (image == nil) {
        return @"";
    }
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSTimeInterval timeInterval = [NSDate date].timeIntervalSince1970 * 1000;

    NSString *imgName = [NSString stringWithFormat:@"image_%.0f_%@.jpg",timeInterval,imageName];
    NSString *image_file = [filePath stringByAppendingPathComponent:imgName];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    [imageData writeToURL:[NSURL fileURLWithPath:image_file] atomically:YES];
    return imgName;
}

+ (void)removeFile:(NSString *)filePath{
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        if (error) {
            LYLog(@"removeFile:%@-%@",filePath,error);
        }
    }

}




//1.Novice Dancer 2.Intermediate Warrior 3.Senior Dominator 4.Elite Leader 5.Legend King 6.Supreme God
+ (NSString *)ballName:(APBallType)type{
    switch (type) {
        case APBallTypeNoviceDancer:
            return @"Novice Dancer";
            break;
        case APBallTypeIntermediateWarrior:
            return @"Intermediate Warrior";
            break;
        case APBallTypeSeniorDominator:
            return @"Senior Dominator";
            break;
        case APBallTypeLeader:
            return @"Elite Leader";
            break;
        case APBallTypeLegendKing:
            return @"Legend King";
            break;
        case APBallTypeSupremeGod:
            return @"Supreme God";
            break;
    }
    return @"";
}

+ (NSString *)ballImageName:(APBallType)type{
    switch (type) {
        case APBallTypeNoviceDancer:
            return @"ball_1";
            break;
        case APBallTypeIntermediateWarrior:
            return @"ball_2";
            break;
        case APBallTypeSeniorDominator:
            return @"ball_3";
            break;
        case APBallTypeLeader:
            return @"ball_4";
            break;
        case APBallTypeLegendKing:
            return @"ball_5";
            break;
        case APBallTypeSupremeGod:
            return @"ball_6";
            break;
    }
    return @"ball_1";
}

+ (void)loadAppConfig{
    [APDataManager getDataNoProcessWithUrlString:appConfig jsonObject:^(NSDictionary * _Nullable jsonDict) {
        if (jsonDict) {
            NSArray *items =jsonDict[@"data"][@"items"];
            for (NSDictionary *item in items) {
                NSString *name = item[@"name"];
                if ([name isEqualToString:@"rck"]) {
                  [APUserDataManager shareInstance].rck = item[@"data"];
                } else if ([name isEqualToString:@"google_translation_key"]) {
                    [APUserDataManager shareInstance].googleTranslationKey = item[@"data"];
                }
            }
        }
    } fail:^(NSError * _Nullable error) {
        
    }];
    
}

@end
